package com.videolist.lingling.ui;

import android.app.ProgressDialog;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.videolist.lingling.R;
import com.videolist.lingling.adapter.ItemClickListener;
import com.videolist.lingling.adapter.Section;
import com.videolist.lingling.adapter.SectionedExpandableLayoutHelper;
import com.videolist.lingling.lingling.widget.utils.widget.TvRecyclerView;
import com.videolist.lingling.models.Item;
import com.yyl.videolist.VideoView;
import com.yyl.videolist.listeners.MediaListenerEvent;
import com.yyl.videolist.utils.V;

import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.vlc.util.VLCOptions;

import java.util.ArrayList;

/**
 * 电视直播
 * 请至官方网站查看更多设置
 * https://wiki.videolan.org/VLC_command-line_help/
 */
public class RTSPActivity extends AppCompatActivity implements MediaListenerEvent, ItemClickListener {
    VideoView videoView;
    String path1 = "rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp";//苹果的m3u8
    String path2 = "rtmp://live.hkstv.hk.lxdns.com/live/hks";// h264的地址
    String path3 = "rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp";//h263地址

    ProgressDialog progressDialog;

    TvRecyclerView mRecyclerView;
    //视频布局
    private LinearLayout layout_video;
    private TextView text_item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtsp);

        progressDialog = new ProgressDialog(this);
        videoView = V.findV(this, R.id.rtsp_video);
        text_item = (TextView) findViewById(R.id.text_item);
        layout_video = (LinearLayout) findViewById(R.id.layout_video);
        videoView.setMediaListenerEvent(this);
//        videoView.startPlay(path2);

//        getSupportActionBar().setTitle("RTSP,电视直播,m3u8");
        mRecyclerView = (TvRecyclerView) findViewById(R.id.recycler_view);
        SectionedExpandableLayoutHelper sectionedExpandableLayoutHelper = new SectionedExpandableLayoutHelper(this,
                mRecyclerView, this, 1);
        mRecyclerView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i("abc", "hasfocus:" + hasFocus);
                /*if (hasFocus) {
                    if (mRecyclerView.getChildCount() > 0) {
                        mRecyclerView.getChildAt(0).requestFocus();
                    }
                }*/
//                mRecyclerView.getChildAt(0).requestFocus();


            }
        });
        //random data
        ArrayList<Item> arrayList = new ArrayList<>();
        arrayList.add(new Item("iPhone", 0));
        arrayList.add(new Item("iPad", 1));
        arrayList.add(new Item("iPod", 2));
        arrayList.add(new Item("iMac", 3));
        sectionedExpandableLayoutHelper.addSection("Apple Products", arrayList);
        arrayList = new ArrayList<>();
        arrayList.add(new Item("LG", 0));
        arrayList.add(new Item("Apple", 1));
        arrayList.add(new Item("Samsung", 2));
        arrayList.add(new Item("Motorola", 3));
        arrayList.add(new Item("Sony", 4));
        arrayList.add(new Item("Nokia", 5));
        sectionedExpandableLayoutHelper.addSection("Companies", arrayList);
        arrayList = new ArrayList<>();
        arrayList.add(new Item("Chocolate", 0));
        arrayList.add(new Item("Strawberry", 1));
        arrayList.add(new Item("Vanilla", 2));
        arrayList.add(new Item("Butterscotch", 3));
        arrayList.add(new Item("Grape", 4));
        sectionedExpandableLayoutHelper.addSection("Ice cream", arrayList);

        sectionedExpandableLayoutHelper.notifyDataSetChanged();

        //checking if adding single item works
        sectionedExpandableLayoutHelper.addItem("Ice cream", new Item("Tutti frutti", 5));
        sectionedExpandableLayoutHelper.notifyDataSetChanged();
    }

    @Override
    protected void onStop() {
        super.onStop();
        videoView.onStop();
    }


    /**
     * <p>尝试秒开网络流但是秒开后马上就卡那几秒
     * <p>有用的上的自己慢慢调吧
     * 推荐硬解会好点  手机烂的就算了
     */
    public void test2() {
//        videoView.onStop();
        ArrayList<String> libOptions = VLCOptions.getLibOptions(getApplicationContext());
        //  libOptions.add("--rtsp-tcp");
        libOptions.add("--rtsp-http");
        libOptions.add("--rtsp-frame-buffer-size=1024");//  --rtsp-frame-buffer-size=<integer [-2147483648 .. 2147483647]>
        //libOptions.add("--rtsp-timeout=1");
        libOptions.add("--ipv4-timeout=5");
        libOptions.add("--network-caching=500");
        libOptions.add("--androidwindow-chroma");
        libOptions.add("RV16");
        LibVLC libVLC = new LibVLC(getApplicationContext(), libOptions);
        videoView.setMediaPlayer(libVLC);

        final Media media = new Media(libVLC, Uri.parse(path2));
        media.setHWDecoderEnabled(true, true);
        media.parseAsync(Media.Parse.FetchNetwork, 10 * 1000);
        media.addOption(":file-caching=500");
        media.addOption(":network-caching=500");
        videoView.setMedia(media);
        videoView.startPlay(path2);

    }

    long time;

    @Override
    public void eventPlayInit(boolean openingVideo) {
        if (!openingVideo) {
            time = System.currentTimeMillis();
        } else {
            long useTime = System.currentTimeMillis() - time;
            Log.i("yyl", "打开地址时间=" + useTime);
        }
        progressDialog.show();
    }

    @Override
    public void eventBuffing(float buffing, boolean show) {

    }


    @Override
    public void eventStop(boolean isPlayError) {
        progressDialog.hide();
    }

    @Override
    public void eventError(int error, boolean show) {
        progressDialog.hide();
    }

    @Override
    public void eventPlay() {
        long useTime = System.currentTimeMillis() - time;
        Log.i("yyl", "延迟时间=" + useTime);
        progressDialog.hide();
    }

    @Override
    public void eventPause() {

    }

    @Override
    public void eventReleaseInit() {

    }

    @Override
    public void itemClicked(Item item) {
        Toast.makeText(this, "Item: " + item.getName() + " clicked", Toast.LENGTH_SHORT).show();
//        videoView.startPlay(path1);
        text_item.setText(item.getName());

    }

    @Override
    public void itemClicked(Section section) {
//        Toast.makeText(this, "Section: " + section.getName() + " clicked", Toast.LENGTH_SHORT).show();
    }
}
